package sensor;

import com.hse.network.IEndDevice;
import com.hse.network.Network;
import com.hse.network.NetworkFactory;
import com.hse.network.exceptions.NetworkException;

/**
 * Created by IntelliJ IDEA.
 * User: kate
 * Date: 01.06.2009
 * Time: 0:51:43
 * To change this template use File | Settings | File Templates.
 */
public class Switcher {
    //my switcher
    private IEndDevice endDevice;
    private Network network;
    private final String type = "s";
    private String name = "s1";
    private String swtch = "OFF";

    // private boolean Connected;
    // private String port;
    // private long macAdd;

    public Switcher(String portName, long macAddress) throws InstantiationException {
        //port = portName;
        //macAdd = macAddress;
        endDevice = /*new EndDevice(portName, macAddress);*/NetworkFactory.createEndDevice(portName, macAddress);
    }

    public Network scan() throws NetworkException {
        network = endDevice.scan()[0];
        return network;
    }

    public void connect() throws NetworkException {
        endDevice.connect(network.getPanId());
        endDevice.send((type + ";" + name).getBytes());
        //Connected = true;
    }

    public void send(String data) throws NetworkException {
        endDevice.send(data.getBytes());
    }

    public String recv() throws NetworkException {
        return new String(endDevice.recv());///.toString();
    }

    public void close() {
        endDevice.close();
        // Connected = false;
    }

    public void setOn() {
        if (this.swtch == "OFF")
            this.swtch = "ON";
    }

    public void setOff() {
        if (this.swtch == "ON")
            this.swtch = "OFF";
    }

    public String get() {
        return this.swtch;
    }
}
